package com.brayan.webapp.controller.realestate;

import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

import com.brayan.webapp.controller.general.i18n.I18N;
import com.brayan.webapp.model.realestate.Reservation;
import com.brayan.webapp.model.realestate.Tenant;
import com.brayan.webapp.model.usermgmt.User;
import com.brayan.webapp.repository.realestate.ReservationRepository;
import com.brayan.webapp.repository.usermgmt.TenantRepository;

@ManagedBean
@RequestScoped
public class UserReservationController {

	@Inject
	@Named("facesContext")
	private FacesContext facesContext;

	@Inject
	private ReservationRepository reservationRepoService;
	@Inject
	private TenantRepository tenantRepoService;
	@Inject
	private I18N i18n;

	public List<Reservation> findAllReservationsByTenantId() {
		User user = (User) facesContext.getExternalContext().getSessionMap().get("user");
		Tenant tenant = null;
		if (user != null) {
			// Check if there is a tenant for this user for additional data.
			tenant = tenantRepoService.findByEmail(user.getUsername());
		}
		if (tenant == null){
			facesContext.addMessage(null, new FacesMessage(i18n.getTrans("YouAreNotLoggedIn")));
			return null;
		}
		else
			return reservationRepoService.getReservationsByTenantId(tenant.getId());
	}
}
